﻿using System;
using Netkk.Common.Models;

namespace Netkk.Models
{
    public class MessageModel
    {
        public Guid Id { get; set; }
        public UserModel FromUser { get; set; }
        public UserModel ToUser { get; set; }
        public string Email { get; set; }
        public string Name { get; set; }
        public String Subject { get; set; }
        public String Text { get; set; }
        public DateTime SendDate { get; set; }

        public static MessageModel Create(UserModel fromUser)
        {
            return new MessageModel
                       {
                           Id = Guid.NewGuid(),
                           FromUser = fromUser,
                           Subject = String.Empty,
                           Text = String.Empty,
                           Name = String.Empty,
                           Email = String.Empty
                       };
        }

        public static MessageModel Create(Message message)
        {
            UserModel fromUser = UserModel.Create(message.FromUser);
            return new MessageModel
            {
                Id = message.Id,
                FromUser = fromUser,
                ToUser = UserModel.Create(message.ToUser),
                Subject = message.Subject,
                Text = message.Text,
                Name = fromUser.ToString(),
                Email = fromUser.Email
            };
        }

        public Message ToMessage()
        {
            if (FromUser == null)
            {
                FromUser = new UserModel
                               {
                                   Name = Name,
                                   Email = Email
                               };
            }
            return new Message
            {
                Id = Id,
                FromUser = FromUser.MapTo(),
                ToUser = ToUser.MapTo(),
                Subject = Subject,
                Text = Text
            };
        }

    }
}
